"""
V1 
按顺序执行命令
固定命令行
"""
import pygame
import vgamepad as vg
import time


# 命令映射
command_map = {
    'w': ('left', 0.0, 1.0, "左摇杆向上移动"),
    's': ('left',0.0, -1.0, "左摇杆向下移动"),
    'a': ('left',-1.0, 0.0, "左摇杆向左移动"),
    'd': ('left',1.0, 0.0, "左摇杆向右移动"),
    '上': ('right', 0.0, 1.0, "右摇杆向上移动"),
    '下': ('right',0.0, -1.0, "右摇杆向下移动"),
    '左': ('right',-1.0, 0.0, "右摇杆向左移动"),
    '右': ('right',1.0, 0.0, "右摇杆向右移动"),
    'A': ('button', vg.XUSB_BUTTON.XUSB_GAMEPAD_A, "A按钮"),
    'B': ('button', vg.XUSB_BUTTON.XUSB_GAMEPAD_B, "B按钮"),
    'X': ('button', vg.XUSB_BUTTON.XUSB_GAMEPAD_X, "X按钮"),
    'Y': ('button', vg.XUSB_BUTTON.XUSB_GAMEPAD_Y, "Y按钮")
}

def execute_command_sequence(command_string):
    print("开始执行命令序列:", command_string)
    
    # 创建虚拟手柄
    gamepad = vg.VX360Gamepad()
    print("虚拟手柄创建成功")
    
    # 执行每个命令
    for i, char in enumerate(command_string):
        if char in command_map:
            command = command_map[char]
            print(f"执行第{i+1}个命令: {char} - {command[2]}")
            
            if command[0] == 'left':  # 左摇杆命令
                _, x_axis, y_axis, _ = command
                gamepad.left_joystick_float(x_value_float=x_axis, y_value_float=y_axis)
                gamepad.update()
                print(f"左摇杆移动到: X={x_axis}, Y={y_axis}")
                time.sleep(0.2)  # 执行1秒
                
                # 重置摇杆
                gamepad.left_joystick_float(x_value_float=0.0, y_value_float=0.0)
                gamepad.update()
                print("左摇杆回中")
            
            elif command[0] == 'right':  # 右摇杆命令
                _, x_axis, y_axis, _ = command
                gamepad.right_joystick_float(x_value_float=x_axis, y_value_float=y_axis)
                gamepad.update()
                print(f"右摇杆移动到: X={x_axis}, Y={y_axis}")
                time.sleep(0.2)  # 执行1秒
                
                # 重置摇杆
                gamepad.right_joystick_float(x_value_float=0.0, y_value_float=0.0)
                gamepad.update()
                print("右摇杆回中")
                
            elif command[0] == 'button':  # 按钮命令
                _, button, _ = command
                gamepad.press_button(button=button)
                gamepad.update()
                print(f"按下按钮: {button}")
                time.sleep(0.1)  # 短暂按下
                
                gamepad.release_button(button=button)
                gamepad.update()
                print(f"释放按钮: {button}")
                time.sleep(0.1)  # 总共1秒
            
            # 命令间短暂间隔
            if i < len(command_string) - 1:
                time.sleep(0.1)
        
    
    # 清理
    gamepad.reset()
    gamepad.update()
    print("命令序列执行完成")

def execute_command_sequence_init(command_string):
    print("开始执行命令序列:", command_string)
    
    # 创建虚拟手柄
    gamepad = vg.VX360Gamepad()
    print("虚拟手柄创建成功")
    
    # 执行每个命令
    for i, char in enumerate(command_string):
        if char in command_map:
            command = command_map[char]
            print(f"执行第{i+1}个命令: {char} - {command[2]}")
            
            if command[0] == 'left':  # 左摇杆命令
                _, x_axis, y_axis, _ = command
                gamepad.left_joystick_float(x_value_float=x_axis, y_value_float=y_axis)
                gamepad.update()
                print(f"左摇杆移动到: X={x_axis}, Y={y_axis}")
                time.sleep(1)  # 执行1秒
                
                # 重置摇杆
                gamepad.left_joystick_float(x_value_float=0.0, y_value_float=0.0)
                gamepad.update()
                print("左摇杆回中")
            
            elif command[0] == 'right':  # 右摇杆命令
                _, x_axis, y_axis, _ = command
                gamepad.right_joystick_float(x_value_float=x_axis, y_value_float=y_axis)
                gamepad.update()
                print(f"右摇杆移动到: X={x_axis}, Y={y_axis}")
                time.sleep(1)  # 执行1秒
                
                # 重置摇杆
                gamepad.right_joystick_float(x_value_float=0.0, y_value_float=0.0)
                gamepad.update()
                print("右摇杆回中")
                
            elif command[0] == 'button':  # 按钮命令
                _, button, _ = command
                gamepad.press_button(button=button)
                gamepad.update()
                print(f"按下按钮: {button}")
                time.sleep(0.9)  # 短暂按下
                
                gamepad.release_button(button=button)
                gamepad.update()
                print(f"释放按钮: {button}")
                time.sleep(0.1)  # 总共1秒
            
            # 命令间短暂间隔
            if i < len(command_string) - 1:
                time.sleep(0.1)
        time.sleep(5)
        
    time.sleep(10)
    
    # 清理
    gamepad.reset()
    gamepad.update()
    print("命令序列执行完成")


def init():
    time.sleep(5)
    command_sequence = "AaA上"
    execute_command_sequence_init(command_sequence)

if __name__ == "__main__":
    # keyboard_to_gamepad()
    # time.sleep(5)
    # command_sequence = "A左A上上下下"
    # execute_command_sequence(command_sequence)
    init()